|
|
|
|
|
|
|
By now, you've learned how to use controls to allow your programs to interact with your users. The controls you've added to forms so far have been independent controls, with no particular relationship to other controls on your forms. As you design more complex user interfaces, however, you will often find it convenient to work with controls as a group. |
|
|
|
|
|
|
|
|
In Chapter 4, ''Using Visual Basic's Default Controls," you learned how to select multiple controls in the design environment and manipulate their properties as a group. Although this type of manipulation is convenient at design time, it does nothing to allow your program to work with groups of controls at runtime. Using control arrays can make runtime manipulation of multiple controls easy. |
|
|
|
|
|
|
|
|
Introducing Control Arrays |
|
|
|
|
|
|
|
|
A control array is a group of controls, all of the same type, that have the same name and are identified by an index. If you are familiar with arrays, you will find that control arrays are similar. |
|
|
|
|
|
|
|
|
Each individual control in the array is referred to as an element of the array. Each element of a control array must meet several criteria: |
|
|
|
|
|
|
|
|
Each control must be of the same type. For example, a control array can contain either labels or text boxes, but cannot contain both. |
|
|
|
|
|
|
|
|
Each control must have the same value for the Name property. |
|
|
|
|
|
|
|
|
Each control is identified by having a unique value for its Index property. The Index property is an Integer type; therefore, its maximum possible value is 32767. |
|
|
|
|
|
|
|
|
Often, a control array's elements share the same values for appearance-related properties such as Font and BorderStyle, but only the Name property is required to be the same. All other properties can be different for each element of the array. |
|
|
|
|
|
|
|
|
Understanding the Advantages of Control Arrays |
|
|
|
|
|
|
|
|
Working with control arrays instead of a group of individual controls presents a number of advantages; they help you in designing the interface of your program and in handling the program code. The following are some of the advantages of control arrays: |
|
|
|
|
|
|
|
|
Adding an element to a control array requires fewer system resources than adding an individual control of the same type. For example, three independent text boxes (Text1, Text2, and Text3) use more resources than a three-element text box array. |
|
|
|
|
|
|
|
|
The elements of a control array share a common set of event procedures. This means you have to write program code in only one place to handle the same event for all controls in the array. |
|
|
|
|
|
|
|
|
Control arrays provide the only means of adding controls to a form while your program is running. |
|
|
|
|
|